package controller;


import com.fasterxml.jackson.databind.util.JSONPObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import pojo.SCMUser;
import response.Result;
import service.SCMUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author Admin
 * @date 2022/11/8 18:06
 * @Description TODO
 * @Version 1.0
 **/
@RestController
public class LoginController {

    @Resource
    private SCMUserService service;

    @PostMapping("login")
    public Result login(@RequestBody SCMUser scmUser, HttpServletRequest request) {
        SCMUser u = service.getUserByAccount(scmUser.getAccount(), scmUser.getPassword());
        if (u == null) {
            return Result.error("账号或密码错误！");
        } else if (u.getStatus() == 1) {
            return Result.error("账号被锁定，请联系管理员处理！");
        } else {
            request.getSession().setAttribute("loginUser",u);
            return Result.ok(u);
        }
    }

    //获取当前登录者信息
    @GetMapping("getAccount")
    public Result getAccount(HttpServletRequest request) {
        SCMUser u = (SCMUser) request.getSession().getAttribute("loginUser");

        return Result.ok(u);
    }

}
